import axios from "axios";
import config from "../../config.json";
import {notification} from "antd";
import ToastUtils from "../utils/ToastUtils.js";

const request = axios.create({
    baseURL: config.request.baseUrl + ":" + config.request.prot,
    timeout: config.request.timout.connect,
    timeoutErrorMessage: config.request.timout.errorMessage
})


/**
 * 相应拦截器
 * @param req  请求对象
 * @param resp  响应对象
 */
request.interceptors.response.use((response) => {
    notification.config({
        placement: 'bottomRight',
        bottom: 50,
        duration: 3,
        rtl: true,
    });
    return new Promise((resolve, reject) => {
        if (response.status === 200) {
            var {code, data, message} = response.data;
            if (code === config.server.successCode) {
                ToastUtils.progressSuccess(message)
                resolve({
                    message,
                    data
                });
            } else {
                ToastUtils.progressError(message)
            }
        } else {
            reject(response);
            ToastUtils.progressError("网络错误")
        }
    })

})


/**
 * get 请求
 *
 * @param url  请求URL
 */
const httpGet = (url) => {
    return request.get(url)
}


/**
 *   post 请求
 *
 * @param url  请求URL
 * @param data  请求数据
 */
const httpPost = (url, data) => {
    return request.post(url, data)
}


export {
    httpGet,
    httpPost
}





